package cc.mrbird.system.service.impl;

import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.service.impl.BaseService;
import cc.mrbird.system.dao.ProjectNumMapper;
import cc.mrbird.system.domain.*;
import cc.mrbird.system.service.ProjectNumService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service("ProjectNumService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ProjectNumServicelmpl extends BaseService<ProjectNum> implements ProjectNumService {
    /**
     * 项目代码的服务sql 接口
     */
    @Autowired
    private ProjectNumMapper projectNumMapper;
    /**
     * 项目代号服务接口
     */
    @Autowired
    private ProjectNumService projectNumService;

    /**
     * 获取项目代码的展示
     *
     * @param projectNum 搜索信息
     * @param request    分页
     * @return
     */
    @Override
    public List<ProjectNum> findProjectNumPage(ProjectNum projectNum, QueryRequest request) {
        List<ProjectNum> projectNumList = this.projectNumMapper.projectNumPage(projectNum);
        return projectNumList;
    }

    /**
     * 添加项目代号
     *
     * @param projectNum 项目代号
     * @return 是否添加成功
     */
    @Override
    public Boolean addProjectNum(ProjectNum projectNum) {
        //当前用户信息
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        //当前时间
        projectNum.setCreationTime(new Date());
        //用户名称
        projectNum.setCreation(user.getUsername());
        //添加名称
        int insert = projectNumService.save(projectNum);
        //是否添加成功
        return insert > 0 ? true : false;
    }

    /**
     * 删除代号
     *
     * @param id 删除的id
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteProjectNum(String id) {
        //获取批量id
        List<String> list = Arrays.asList(id.split(","));
        //删除
        int i = this.batchDelete(list, "id", ProjectNum.class);
        //是否成功
        return i > 0 ? true : false;
    }

    /**
     * 修改代号
     *
     * @param projectNum 修改信息
     * @return 是否删除成功
     */
    @Override
    public Boolean updateProjectNum(ProjectNum projectNum) {
        //更新信息
        int i = this.updateNotNull(projectNum);
        //是否更新成功
        return i > 0 ? true : false;
    }
}
